using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CaliburnWpf.ViewModels.Dictionaries
{
    using Infrastructure;
    using System.Collections.ObjectModel;
    using MoneyManagerNET.Presentation.ViewModel.Base;
    using MoneyManagerNET.Model;

    
    public class CategoryListViewModel:ListViewModel<ICategory>//ViewModelBase
    {

        public CategoryListViewModel():base( App.Storage.Select<ICategory>())
        {
            
        }

        public ICategory Category { get; set; }
        public ISubCategory SubCategory { get; set; }

        object _selectedValue;
        public object SelectedValue {
            get {
                return _selectedValue;
            }
            set {
                _selectedValue = value;
                if (SelectedValue is ICategory) {
                    var selected_category=SelectedValue as ICategory;
                    this.Category = selected_category;
                    this.SubCategory = null;

                    this.CategoryName = selected_category.Name;
                }
                else if (SelectedValue is ISubCategory)
                {
                    var selected_subcategory=SelectedValue as ISubCategory;
                    this.SubCategory = selected_subcategory;
                    this.Category = this.SubCategory.Category as ICategory;

                    this.CategoryName = selected_subcategory.Name;
                }
                else {
                    this.Category = null;
                    this.SubCategory = null;
                }
                

                RefreshCommandStatuses();
            }

        }

        

        #region Commands

        #region new
        public bool CanNew{get{ return SubCategory==null;}}
        public void New()
        {
            if (SubCategory == null)
            {
                if (Category == null)
                {
                    var newCategory = App.Storage.New<ICategory>();
                    newCategory.Name = this.CategoryName;
                    Items.Add(newCategory);
                }
                else
                {
                    var newSubCategory = App.Storage.New<ISubCategory>();
                    newSubCategory.Name = this.CategoryName;
                    newSubCategory.Category = Category;
                    
                    RaiseCollectionChanged(Category.SubCategoryList);//TODO:
                }
            }
            this.NotifyOfPropertyChange(() => Items);
        }

        #endregion

        #region edit
        public bool CanEdit{get{return !(SubCategory==null && Category==null);}}
        public void Edit()
        {
            if (Category != null)
            {
                if (SubCategory != null)
                {                    
                    SubCategory.Name = this.CategoryName;
                    RaisePropertyChanged(SubCategory, "Name");//TODO:
                }
                else
                {
                    this.Category.Name=this.CategoryName;
                    RaisePropertyChanged(Category, "Name");
                }
            }
        }

        
        #endregion


        #region delete
        public bool CanDelete { get { return !(SubCategory == null && Category == null); } }
        public void Delete()
        {
            if (Category != null)
            {
                if (SubCategory != null)
                {
                    this.Category.SubCategoryList.Remove(SubCategory);
                    //base.RaiseCollectionChanged(Category.SubCategoryList);//TODO:
                    App.Storage.Delete(SubCategory);
                }
                else
                {
                    App.Storage.Delete(Category);
                    base.Items.Remove(Category);
                    this.NotifyOfPropertyChange(() => Items);
                }
            }
        }
        #endregion

        #endregion
        public bool CanSelect { get { return CanEdit; } }
        public void Select() {
            TryClose(true);
        }

        string _CategoryName;
        public string CategoryName { get { return _CategoryName; } set { _CategoryName = value; this.NotifyOfPropertyChange(() => CategoryName); } }


        protected override void RefreshCommandStatuses()
        {
            base.RefreshCommandStatuses();
            this.NotifyOfPropertyChange(()=>CanNew);
            this.NotifyOfPropertyChange(() => CanEdit);
            this.NotifyOfPropertyChange(() => CanSelect);
        }
    }
}
